<?php

namespace oa\controllers;

use Yii;

class ToolController extends BaseController
{
    public $layout = '../tool/layout';

    public function actionIndex()
    {
        return $this->fetch();
    }

    public function actionPhpinfo()
    {
        return $this->fetch();
    }

    public function actionPhpini()
    {
        return $this->fetch();
    }

    public function actionCookie()
    {
        return $this->fetch();
    }

    public function actionSession()
    {
        return $this->fetch();
    }

    public function actionIp()
    {
        return $this->fetch();
    }

    public function actionYybquery()
    {
        $id = $this->request->get('id');
        $level = $this->request->get('level');
        $list = [];
        if (!empty($id)) {
            $where = "Id like '{$id}%'";
            if (!empty($level)) {
                $where .= " AND CodeLevel=" . $level;
            }
            $list = Yii::$app->db_yisu->createCommand("SELECT * FROM ebcProductionCode WHERE {$where}")->queryAll();
        }
        return $this->fetch(['list' => $list]);
    }

    public function actionYybbulu()
    {
        $post = [
            'id' => '',
            'level' => 1,
            'root_no' => '',
            'box_no' => '',
            'batch' => '',
            'pack_date' => '',
            'factory_id' => '9e0c70cc-a631-4408-8daf-529cbb629e95',
            'material_id' => '1f02d5dd-2c0f-4dab-845f-a9323d99f204',
            'should_org_id' => '017c4c72-00a7-4610-b7dc-ab2f2cd75466',
        ];
        if ($this->request->isPost) {
            $post = $this->request->post();
            if (empty($post['id'])) {
                Yii::$app->session->setFlash("error", "营养包编号不能为空");
            }
            if (empty($post['batch'])) {
                if ($post['level'] == 1) {
                    $post['batch'] = substr($post['id'], 0, 10);
                } elseif ($post['level'] == 2) {
                    $post['batch'] = substr($post['id'], 8, 10);
                }
            }
            if (!preg_match("/^\d{9}A$/", $post['batch'])) {
                Yii::$app->session->setFlash("error", "营养包批号不正确");
            }
            if (empty($post['pack_date'])) {
                $post['pack_date'] = '20' . substr($post['batch'], 0, 2) . '-' . substr($post['batch'], 2, 2) . '-' . substr($post['batch'], 4, 2) . ' 00:00:00.000';
            }
            
            if (!Yii::$app->session->hasFlash('error')) {
                $date = date('Y-m-d') . ' 08:00:00.000';
                $validDate = date('Y-m-d', strtotime('+ 18 months', strtotime($post['pack_date']))) . ' 00:00:00.000';
                $id = str_replace(array("\r\n","\r","\n"),",", $post['id']);
                $id_arr = explode(',', $id);
                foreach ($id_arr as $v) {
                    if (!Yii::$app->db_yisu->createCommand("SELECT 1 FROM ebcProductionCode WHERE Id like '{$v}%'")->queryOne()) {
                        $sql = "INSERT INTO ebcproductioncode(Id,AddBy,AddTime,EditBy,EditTime,BatchCode,CodeLevel,CodePath,Count,FactoryId,IsRunning,LineId,MaterialId,MinSaleUnit,PackDate,ParentCode,PrintDate,RootCode,Sequence,ShouldOrgId,ShouldOrgType,ValidDate)
                    VALUES
                    ('{$v}','hpp','{$date}','hpp','{$date}','{$post['batch']}',{$post['level']},'{$post['root_no']},{$post['box_no']}',1,'{$post['factory_id']}',0,1,'{$post['material_id']}',1,'{$post['pack_date']}','{$post['box_no']}','{$post['pack_date']}','{$post['root_no']}',1,'{$post['should_org_id']}',3,'{$validDate}');";
                        if (@Yii::$app->db_yisu2->createCommand($sql)->execute()) {
                            Yii::$app->session->setFlash("success", "提交成功");
                        } else {
                            Yii::$app->session->setFlash("error", "提交失败，请重试");
                        }
                    } else {
                        Yii::$app->session->setFlash("error", "请勿重复提交");
                    }
                }
            }
        }
        return $this->fetch(['post' => $post]);
    }

    public function actionYyboutquery()
    {
        $id = $this->request->get('id');
        $send_date = '';     
        $headerId = '';     
        $bill = Yii::$app->db_yisu->createCommand("SELECT HeaderId FROM ebtBillData WHERE ProductionCodeId='{$id}'")->queryOne();
        if (!empty($bill)) {
            $headerId = $bill['HeaderId'];
            $bill_header = Yii::$app->db_yisu->createCommand("SELECT AddTime FROM ebtBillHeader WHERE Id='{$headerId}'")->queryOne();
            if (!empty($bill_header)) {
                $send_date = substr($bill_header['AddTime'], 0, 10);
            }
        }
        return $this->fetch(['send_date' => $send_date, 'HeaderId' => $headerId]);
    }

    public function actionYyboutbulu()
    {
        $post = [
            'ProductionCodeId' => '',
            'AddTime' => '',
            'HeaderId' => md5(time()),
        ];
        if ($this->request->isPost) {
            $post = $this->request->post();
            if (empty($post['ProductionCodeId'])) {
                Yii::$app->session->setFlash("error", "营养包编号不能为空");
            }
            if (empty($post['HeaderId'])) {
                Yii::$app->session->setFlash("error", "出库单ID不能为空");
            }
            if (empty($post['AddTime'])) {
                Yii::$app->session->setFlash("error", "出厂日期不能为空");
            }
            $strlen = strlen($post['ProductionCodeId']);
            if ($strlen == 22) {
                $batch = substr($post['ProductionCodeId'], 8, 10);
                if (!preg_match("/^\d{17}A\d{4}$/", $post['ProductionCodeId'])) {
                    Yii::$app->session->setFlash("error", "营养包编号不正确");
                }
            } elseif ($strlen == 16) {
                $batch = substr($post['ProductionCodeId'], 0, 10);
                if (!preg_match("/^\d{9}A\d{6}$/", $post['ProductionCodeId'])) {
                    Yii::$app->session->setFlash("error", "营养包编号不正确");
                }
            }
            if (!Yii::$app->session->hasFlash('error')) {
                if (!Yii::$app->db_yisu->createCommand("SELECT 1 FROM ebtBillData WHERE ProductionCodeId='{$post['ProductionCodeId']}'")->queryOne()) {
                    $date = date('Y-m-d') . ' 08:00:00.000';
                    if (!Yii::$app->db_yisu->createCommand("SELECT 1 FROM ebtBillHeader WHERE Id='{$post['HeaderId']}'")->queryOne()) {
                        $sql = "INSERT INTO ebtBillHeader(Id,AddBy,AddTime,EditTime)
                            VALUES
                            ('{$post['HeaderId']}','hpp','{$post['AddTime']}','{$date}')";
                        if (@Yii::$app->db_yisu2->createCommand($sql)->execute()) {
                        } else {
                            Yii::$app->session->setFlash("error", "提交失败，请重试");
                        } 
                    }
                    if (!Yii::$app->session->hasFlash('error')) {
                        $Id = md5(time() . rand(1000, 9999));
                        $sql = "INSERT INTO ebtBillData(Id,HeaderId,AddBy,AddTime,ProductionCodeId,ScanQty)
                            VALUES
                            ('{$Id}','{$post['HeaderId']}','hpp','{$date}','{$post['ProductionCodeId']}','99')";
                        if (@Yii::$app->db_yisu2->createCommand($sql)->execute()) {
                            Yii::$app->session->setFlash("success", "提交成功");
                        } else {
                            Yii::$app->session->setFlash("error", "出厂提交失败，请重试");
                        }
                    }
                } else {
                    Yii::$app->session->setFlash("error", "请勿重复提交");
                }
            }
        }
        return $this->fetch(['post' => $post]);
    }
}